package com.example.tools;

import java.util.jar.Attributes;

import com.benchi.ncar.R;


import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.animation.Animation;
import android.view.animation.CycleInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.EditText;

public class ClearEditText extends EditText implements OnFocusChangeListener,
		TextWatcher {

	private Drawable mClearDrawalbe;

	private boolean hasFoucs;
	
	private Handler handler;

	public Handler getHandler() {
		return handler;
	}

	public void setHandler(Handler handler) {
		this.handler = handler;
	}

	public ClearEditText(Context context) {
		super(context, null);
	}

	public ClearEditText(Context context, AttributeSet attrs) {
		this(context, attrs, android.R.attr.editTextStyle);
	}

	public ClearEditText(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	private void init() {
		mClearDrawalbe = getCompoundDrawables()[2];
		if (mClearDrawalbe == null) {
			mClearDrawalbe = getResources().getDrawable(R.drawable.rmove);
		}
		mClearDrawalbe.setBounds(0, 0, mClearDrawalbe.getIntrinsicWidth(),
				mClearDrawalbe.getIntrinsicHeight());
		setClearIconVisible(false);

		setOnFocusChangeListener(this);

		addTextChangedListener(this);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_UP) {
			if (getCompoundDrawables()[2] != null) {
				boolean touchable = event.getX() > (getWidth() - getTotalPaddingRight())
						&& event.getX() < (getWidth() - getPaddingRight());
				if (touchable) {
					if (getHandler()==null) {
						this.setText("");	
					}else {
						getHandler().sendEmptyMessage(1);
					}
					
				}
				
			}
		}
		return super.onTouchEvent(event);
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int count, int after) {
		if (hasFoucs) {
			setClearIconVisible(s.length() > 0);
		}
	}

	@Override
	public void afterTextChanged(Editable arg0) {

	}

	@Override
	public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
			int arg3) {

	}

	@Override
	public void onFocusChange(View arg0, boolean hasFocus) {
		this.hasFoucs = hasFocus;
		if (hasFocus) {
			setClearIconVisible(getText().length() > 0);
		} else {
			setClearIconVisible(false);
		}
	}

	private void setClearIconVisible(boolean visible) {
		Drawable right =  mClearDrawalbe ;
		setCompoundDrawables(getCompoundDrawables()[0],
				getCompoundDrawables()[1], right, getCompoundDrawables()[3]);
	}
	
	public void setShakeAnimation(){
		startAnimation(shakeAnimation());
	}
	
	private  Animation shakeAnimation(){
		Animation translateAnimation=new TranslateAnimation(0, 10, 0, 0);
		translateAnimation.setInterpolator(new CycleInterpolator(100));
		translateAnimation.setDuration(1000);
		return translateAnimation;
	}

}
